/*
* @(#)MaintDmsDocumentVersionAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.web;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.DmsErrorConstant;
import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.dms.dao.DmsVersionDAObject;
import com.dcivision.dms.dao.MtmDocumentRelationshipDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.SysUserDefinedIndex;
import com.dcivision.framework.web.AbstractActionForm;
import com.dcivision.framework.web.AbstractMaintAction;

/**
MaintDmsDocumentVersionAction.java

This class is for maint dms dmsDocument action

  @author          Anson mo
  @company         DCIVision Limited
  @creation date   07/11/2005
  @version         $Revision: 1.5 $
*/
public class MaintDmsDocumentVersionAction extends AbstractMaintAction {
	
	public static final String REVISION = "$Revision: 1.5 $";

	/**
	 * execute - Override the parent function.
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)
			throws ServletException {
		MaintDmsDocumentVersionForm actionForm = (MaintDmsDocumentVersionForm) form;
		String opMode = actionForm.getOpMode();
		String navMode = actionForm.getNavMode();

	    request.setAttribute("documentID", actionForm.getID());
		if (!Utility.isEmpty(navMode) && navMode.equals(GlobalConstant.NAV_MODE_PREVIEW)) {
			if (!Utility.isEmpty(request.getParameter("sCurFunctionCode"))) {
				actionForm.setCurFunctionCode(request.getParameter("sCurFunctionCode"));
			}
			this.selectRecord(mapping, actionForm, request, response);
			return mapping.findForward(navMode);
		} else {
			return super.execute(mapping, form, request, response);
		}
	}

	public String getFunctionCode() {
		return(SystemFunctionConstant.DMS_PERSONAL_FOLDER);
	}

	public String getMajorDAOClassName() {
		return ("com.dcivision.dms.dao.DmsDocumentDAObject");
	}

	public void selectRecord(ActionMapping mapping, AbstractActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws ApplicationException {
		MaintDmsDocumentVersionForm docVersionForm = (MaintDmsDocumentVersionForm)form; 
		SessionContainer sessionContainer = this.getSessionContainer(request);
		Connection conn = this.getConnection(request);
		DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
		
		Integer docID = TextUtility.parseIntegerObj(docVersionForm.getID()); 
		DmsDocument dmsDocument = docRetrievalManager.getDocument(docID);
		String docType = dmsDocument.getDocumentType();
		Integer rootID = dmsDocument.getRootID();
			
		if (DmsDocument.DOCUMENT_LINK.equals(docType)){
			MtmDocumentRelationshipDAObject docRelationshipDAO = new MtmDocumentRelationshipDAObject(sessionContainer, conn);
			Integer targetDocID = docRelationshipDAO.getTargetDocIDByRelatedDocID(docID, docType);
	        DmsDocument targetDoc = docRetrievalManager.getDocument(targetDocID);
			if (targetDoc == null || GlobalConstant.RECORD_STATUS_INACTIVE.equals(targetDoc.getRecordStatus())) {
				this.addError(request,DmsErrorConstant.TARGET_DOCUMENT_NOT_FOUND);
			}
			request.setAttribute("targetDoc",targetDoc);
		} 
		else if (DmsDocument.COMPOUND_DOC_TYPE.equals(docType)){
			List compoundDocList = new ArrayList();
			compoundDocList = docRetrievalManager.getDocumentListByParentIDRootID(docID,rootID);
			request.setAttribute("compoundDocList",compoundDocList);
		}			
		
        //Use for List the target User Defined Fields Detail
		List docDetailList = docRetrievalManager.getDocumentDetailList(docID);
		SysUserDefinedIndex sysUserDefinedIndex =  docRetrievalManager.getUserDefinedFieldByDocument(dmsDocument);
		if (sysUserDefinedIndex != null)
		  docVersionForm.setUserDefinedType(sysUserDefinedIndex.getUserDefinedType());
        if (docDetailList!=null){
            docVersionForm.setFormData(dmsDocument, docDetailList);
        } else {
        	docVersionForm.setFormData(dmsDocument);
        }
        //  Use for List the target User Defined Fields Detail
        if (docVersionForm.getUserDefinedFieldID() != null){
            List udfDetailList = docRetrievalManager.getUDFDetailList(TextUtility.parseIntegerObj(docVersionForm.getUserDefinedFieldID()));
            docVersionForm.setUdfDetailList(udfDetailList);
        }

		//get DmsVersion list		
		DmsVersionDAObject dmsVersionDAO = new DmsVersionDAObject(sessionContainer, conn);
		ListDmsVersionForm dmsVersionForm = new ListDmsVersionForm();
		dmsVersionForm.setPageOffset(docVersionForm.getPageOffset());
		dmsVersionForm.setCurStartRowNo(docVersionForm.getCurStartRowNo());
		dmsVersionForm.setDocumentID(docID.toString());
		List dmsVersionList = dmsVersionDAO.getFullList(dmsVersionForm);
		request.setAttribute("dmsVersionList", dmsVersionList);
		docRetrievalManager.release();	
	}
}
